package com.hkts.naturalgas.dictionaries.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.datahandler.constance.EnviromentConstance;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries;
import com.hkts.naturalgas.dictionaries.mapper.HktsEnvironmentProtectionDictionariesMapper;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionDictionariesService;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-06-23
 */
@Service
@Transactional
public class HktsEnvironmentProtectionDictionariesServiceImpl extends ServiceImpl<HktsEnvironmentProtectionDictionariesMapper, HktsEnvironmentProtectionDictionaries> implements IHktsEnvironmentProtectionDictionariesService {
    @Autowired
    public DBUtils dBUtils;

    @Autowired
    private HktsEnvironmentProtectionDictionariesMapper hktsEnvironmentProtectionDictionariesMapper;

    @Override
    public IPage<HktsEnvironmentProtectionDictionaries> selectPage(Page<HktsEnvironmentProtectionDictionaries> page, HktsEnvironmentProtectionDictionaries hktsEnvironmentProtectionDictionaries) {
        QueryWrapper<HktsEnvironmentProtectionDictionaries> query = Wrappers.query();
        QueryWrapper<HktsEnvironmentProtectionDictionaries> entityWrapper = getEntityWrapper(query, hktsEnvironmentProtectionDictionaries);
        return hktsEnvironmentProtectionDictionariesMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsEnvironmentProtectionDictionaries> selectList(HktsEnvironmentProtectionDictionaries hktsEnvironmentProtectionDictionaries) {
        QueryWrapper<HktsEnvironmentProtectionDictionaries> query = Wrappers.query();
        QueryWrapper<HktsEnvironmentProtectionDictionaries> entityWrapper = getEntityWrapper(query, hktsEnvironmentProtectionDictionaries);
        return hktsEnvironmentProtectionDictionariesMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsEnvironmentProtectionDictionaries> getEntityWrapper(QueryWrapper<HktsEnvironmentProtectionDictionaries> entityWrapper, HktsEnvironmentProtectionDictionaries hktsEnvironmentProtectionDictionaries) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionDictionaries.getTableDescribe())) {
            entityWrapper.like(HktsEnvironmentProtectionDictionaries.TABLE_DESCRIBE, hktsEnvironmentProtectionDictionaries.getTableDescribe());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionDictionaries.getTableName())) {
            entityWrapper.like(HktsEnvironmentProtectionDictionaries.TABLE_NAME, hktsEnvironmentProtectionDictionaries.getTableName());
        }
        if (StringUtils.isNotBlank(hktsEnvironmentProtectionDictionaries.getWorkShop())) {
            entityWrapper.like(HktsEnvironmentProtectionDictionaries.WORK_SHOP, hktsEnvironmentProtectionDictionaries.getWorkShop());
        }

        if (StringUtils.isNotBlank(hktsEnvironmentProtectionDictionaries.getIsExport())) {
            entityWrapper.eq(HktsEnvironmentProtectionDictionaries.IS_EXPORT, hktsEnvironmentProtectionDictionaries.getIsExport());
        }

        if (StringUtils.isNotBlank(hktsEnvironmentProtectionDictionaries.getIsEnable())) {
            entityWrapper.eq(HktsEnvironmentProtectionDictionaries.IS_ENABLE, hktsEnvironmentProtectionDictionaries.getIsEnable());
        }
        return entityWrapper;
    }

    @Override
    public List<HktsEnvironmentProtectionDictionaries> getTableByWorkShop(String workShop,String summary) {
        QueryWrapper<HktsEnvironmentProtectionDictionaries> query = Wrappers.query();
        query.lambda().eq(HktsEnvironmentProtectionDictionaries::getWorkShop, workShop);
        query.lambda().eq(StringUtils.isNotBlank(summary),HktsEnvironmentProtectionDictionaries::getSummary,summary);
        query.lambda().eq(HktsEnvironmentProtectionDictionaries::getIsEnable, EnviromentConstance.IS_ENABLE);
        return hktsEnvironmentProtectionDictionariesMapper.selectList(query);
    }

    @Override
    public List<HktsEnvironmentProtectionDictionaries> getHttpTable() {
        QueryWrapper<HktsEnvironmentProtectionDictionaries> query = Wrappers.query();
        query.lambda().eq(HktsEnvironmentProtectionDictionaries::getIsHttp, EnviromentConstance.IS_HTTP);
        List<HktsEnvironmentProtectionDictionaries> list= hktsEnvironmentProtectionDictionariesMapper.selectList(query);
        return list;
    }

    @Override
    public List<HktsEnvironmentProtectionDictionaries> getDbTable() {
        QueryWrapper<HktsEnvironmentProtectionDictionaries> query = Wrappers.query();
        query.lambda().eq(HktsEnvironmentProtectionDictionaries::getIsHttp, EnviromentConstance.IS_MYSQL);
        List<HktsEnvironmentProtectionDictionaries> list= hktsEnvironmentProtectionDictionariesMapper.selectList(query);
        return list;
    }

    @Override
    public HktsEnvironmentProtectionDictionaries getTableId(String tableId,String isExport,String isEnable) {
        QueryWrapper<HktsEnvironmentProtectionDictionaries> query = Wrappers.query();
        query.lambda().eq(HktsEnvironmentProtectionDictionaries::getId, tableId)
        .eq(HktsEnvironmentProtectionDictionaries::getIsEnable,isEnable)
        .eq(HktsEnvironmentProtectionDictionaries::getIsExport,isExport);
        HktsEnvironmentProtectionDictionaries hepd = hktsEnvironmentProtectionDictionariesMapper.selectOne(query);
        return hepd;
    }
}
